package com.bilibili.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilibili.entity.CommenResult;
import com.bilibili.service.CommentService;
import com.bilibili.service.impl.CommentServiceImpl;

@WebServlet("/deleteComment.do")
public class DeleteCommentServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        String commentIdStr = request.getParameter("commentId");
        if (commentIdStr != null && !commentIdStr.isEmpty()) {
            try {
                int commentId = Integer.parseInt(commentIdStr);
                CommentService cs = new CommentServiceImpl();
                boolean result = cs.deleteComment(commentId);
                CommenResult commenResult = new CommenResult();
                if (result) {
                    commenResult.setCode(200);
                    commenResult.setMessage("评论删除成功");
                } else {
                    commenResult.setCode(500);
                    commenResult.setMessage("评论删除失败");
                }
                response.getWriter().write(commenResult.toJOSNString());
            } catch (NumberFormatException e) {
                CommenResult commenResult = new CommenResult();
                commenResult.setCode(500);
                commenResult.setMessage("参数格式错误");
                response.getWriter().write(commenResult.toJOSNString());
            }
        } else {
            CommenResult commenResult = new CommenResult();
            commenResult.setCode(500);
            commenResult.setMessage("缺少评论ID参数");
            response.getWriter().write(commenResult.toJOSNString());
        }
    }
}